<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\cms\controller\emailRecord;

use plugin\saiadmin\basic\BaseController;
use app\cms\logic\emailRecord\ProjectMeetingInvitationEmailRecordLogic;
use app\cms\validate\emailRecord\ProjectMeetingInvitationEmailRecordValidate;
use support\Log;
use support\Request;
use support\Response;

/**
 * 邮件记录控制器
 */
class ProjectMeetingInvitationEmailRecordController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new ProjectMeetingInvitationEmailRecordLogic();
        $this->validate = new ProjectMeetingInvitationEmailRecordValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['email_type', ''],
            ['project_meeting_invitation_speaker_id', '']
        ]);

        $query = $this->logic->search($where);
        $query->whereIn('email_type', '1,2,3');
        $data = $this->logic->getList($query);
        return $this->success($data);
    }


    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function list(Request $request): Response
    {
        $where = $request->more([
            ['email_type', ''],
            ['project_meeting_invitation_speaker_id', '']
        ]);

        $query = $this->logic->search($where);
        $query->where('email_type', '4');
        $data = $this->logic->getList($query);
        return $this->success($data);
    }


    /**
     * 保存数据
     * @param Request $request
     * @return Response
     */
    public function save(Request $request): Response
    {
        $data = $request->post();

        Log::info('保存数据：' . json_encode($data));

        $result = $this->logic->save($data);
        if ($result) {
            // $res = $this->afterChanges($data);
            $res = $this->logic->order('id', 'desc')->find();
            return $this->success([$res->id], '操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }
}
